import java.util.HashMap;
import java.util.Scanner;

public class demo_01 {

    static String screen = "";
    static String clipboard = "";
    static String selected = "";

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] chars = sc.nextLine().split(" ");
        for (int i = 0; i < chars.length; i++) {
            sendCommand(chars[i]);
        }
        System.out.println(screen.length());
    }

    private static void sendCommand(String command) {
        if(command.equals("1")){
            if(selected.length() > 0){
                screen = selected;
            }
            else{
                screen += "a";
            }
        }else if(command.equals("2")){
            if(selected.length() > 0){
                clipboard = selected;
            }
           
        }else if(command.equals("3")){
            if(selected.length() > 0){
                clipboard = selected;
                selected = "";
            }
        }else if(command == "4"){
            if(selected.length() > 0){
                screen = clipboard;
            }else{
                screen += clipboard;
            }
           
        }else if(command.equals("5")){
            if(screen.length() > 0){
                selected = screen;
            }
        }
    }
}